package com.lenovo.smarttraffic.ui.activity.stage5;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.BusLineInfoBean;
import com.lenovo.smarttraffic.bean.BusStationInfoBean;
import com.lenovo.smarttraffic.bean.SensorData;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;

import java.util.List;

public class RealBusActivity extends BaseActivity {

    public static List<BusLineInfoBean.ROWSDETAILBean> lineList;
    public static List<BusStationInfoBean.ROWSDETAILBean> stationList;
    public static SensorData sensorData;

    private Toolbar toolbar;

    private Spinner id_sp;
    private Button find_btn;
    private TextView distant_tv, time_tv;
    private ExpandableListView listView;
    private RealBusAdapter adapter;
    private Button refresh_btn;

    @Override
    protected int getLayout() {
        return R.layout.activity_real_bus;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener((v) -> finish());

        id_sp = findViewById(R.id.id_sp);
        find_btn = findViewById(R.id.find_btn);
        distant_tv = findViewById(R.id.distant_tv);
        time_tv = findViewById(R.id.time_tv);
        listView = findViewById(R.id.ex_listView);
        refresh_btn = findViewById(R.id.refresh_btn);

        initData();
    }

    private void initData() {
        HttpUtils.post("get_busline_info", new HttpUtils().add("UserName", MainActivity.userName), response -> {
            BusLineInfoBean busLineInfoBean = new Gson().fromJson(response.toString(), BusLineInfoBean.class);
            lineList = busLineInfoBean.getROWS_DETAIL();

            refresh();
        });

        id_sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                distant_tv.setText("距离" + (position + 1) + "号站台的距离：");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        find_btn.setOnClickListener(v -> refresh());
        refresh_btn.setOnClickListener(v -> refresh());
    }

    private void refresh() {
        HttpUtils.post("get_all_sense", new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> HttpUtils.post("get_bus_station_info", new HttpUtils().add("UserName", MainActivity.userName), response2 -> {
                    BusStationInfoBean busStationInfoBean = new Gson().fromJson(response2.toString(), BusStationInfoBean.class);
                    stationList = busStationInfoBean.getROWS_DETAIL();
                    sensorData = new Gson().fromJson(response.toString(), SensorData.class);

                    if (adapter == null) {
                        adapter = new RealBusAdapter();
                        listView.setAdapter(adapter);
                        listView.setOnGroupClickListener((parent, v, groupPosition, id) -> {
                            LinearLayout time_parent = v.findViewById(R.id.time_parent);
                            time_parent.setVisibility(time_parent.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);

                            if (!parent.isGroupExpanded(groupPosition) && time_tv.getVisibility() == View.VISIBLE) {
                                time_tv.setVisibility(View.INVISIBLE);
                            } else if (parent.isGroupExpanded(groupPosition)) {
                                time_tv.setVisibility(View.VISIBLE);
                            }
                            return false;
                        });
                    } else {
                        adapter.notifyDataSetChanged();
                    }
                }));
    }

}

class RealBusAdapter extends BaseExpandableListAdapter {

    public RealBusAdapter() {
    }

    @Override
    public int getGroupCount() {
        return 2;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
        return true;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_realbus_group, parent, false);
            viewHolder = new GroupViewHolder();
            viewHolder.time_parent = convertView.findViewById(R.id.time_parent);
            viewHolder.distant_tv = convertView.findViewById(R.id.distant_tv);
            viewHolder.time_tv = convertView.findViewById(R.id.time_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }

        int distant = RealBusActivity.stationList.get(groupPosition).getDistance() / 10;
        viewHolder.distant_tv.setText((groupPosition + 1) + "号公交车：" + distant + "m");
        viewHolder.time_tv.setText((distant / 1000) + "分钟");

        return convertView;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_realbus_child, parent, false);
            viewHolder = new ChildViewHolder();
            viewHolder.value = convertView.findViewById(R.id.value);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }

        SensorData sensorData = RealBusActivity.sensorData;
        BusLineInfoBean.ROWSDETAILBean lineInfoBean = RealBusActivity.lineList.get(groupPosition);
        int min = RealBusActivity.stationList.get(groupPosition).getDistance() / 10000;

        viewHolder.value.setText("首末班车：" + lineInfoBean.getStart_place_start_time() + "——" + lineInfoBean.getStart_place_end_time() + "  预计" + min + "分钟到站\n" +
                "  候车环境：\n" + "    PM2.5：" + sensorData.getPM25() + "ug/m3，温度：" + sensorData.getTemperature() + "℃\n" +
                "    湿度：" + sensorData.getHumidity() + "%，CO2：" + sensorData.getCo2() + "ppm");

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //指定位置上的子元素是否可选中
        return true;
    }

    static class GroupViewHolder {
        LinearLayout time_parent;
        TextView distant_tv, time_tv;
    }

    static class ChildViewHolder {
        TextView value;
    }
}